package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyjhsoc.game.dao.CompetitionSmallGameDAO;
import com.iyjhsoc.game.dao.CompetitionSmallTeamLoLInfoDAO;
import com.iyjhsoc.game.domain.VO.CompetitionSmallGameVO;
import com.iyjhsoc.game.domain.VO.CompetitionSmallTeamLoLInfoVO;
import com.iyjhsoc.game.domain.dto.CompetitionSmallTeamLoLInfoDTO;
import com.iyjhsoc.game.domain.entity.CompetitionSmallTeamLoLInfoDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.CompetitionSmallTeamLoLInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CompetitionSmallTeamLoLInfoServiceImpl extends ServiceImpl<CompetitionSmallTeamLoLInfoDAO, CompetitionSmallTeamLoLInfoDO> implements CompetitionSmallTeamLoLInfoService {
    @Resource
    CompetitionSmallTeamLoLInfoDAO dao;
    @Resource
    CompetitionSmallGameDAO competitionSmallGameDAO;
    @Override
    public Response queryPage(CompetitionSmallGameVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<CompetitionSmallGameVO> list = competitionSmallGameDAO.queryCompetitionToGame(vo);
        List<Long> gameIds = new ArrayList<>();
        for(CompetitionSmallGameVO gameInfo:list){
            gameIds.add(gameInfo.getId());
        }
        List<CompetitionSmallTeamLoLInfoDTO> competitionSmallTeamLoLInfoDTOS = dao.queryListBySmallId(gameIds);
        Map<Long,List<CompetitionSmallTeamLoLInfoDTO>> res = new HashMap<>();
        for(CompetitionSmallTeamLoLInfoDTO dto:competitionSmallTeamLoLInfoDTOS){
            if(res.containsKey(dto.getSmallGameId())){
                               List<CompetitionSmallTeamLoLInfoDTO> dtos = res.get(dto.getSmallGameId());
                dtos.add(dto);
            } else {
                List<CompetitionSmallTeamLoLInfoDTO> dtos = new ArrayList<>();
                dtos.add(dto);
                res.put(dto.getSmallGameId(),dtos);
            }
        }
        List<List<CompetitionSmallTeamLoLInfoDTO>> gameInfo = new ArrayList<>();
        for(CompetitionSmallGameVO smallGame:list){
            if(res.containsKey(smallGame.getId())) {
                gameInfo.add(res.get(smallGame.getId()));
            } else {
                List<CompetitionSmallTeamLoLInfoDTO> temporary = createTemporary(smallGame);
                gameInfo.add(temporary);
            }
        }
        return Response.SUCCESS(new PageInfo<List<CompetitionSmallTeamLoLInfoDTO>>(gameInfo));
    }

    public List<CompetitionSmallTeamLoLInfoDTO> createTemporary(CompetitionSmallGameVO vo){
        List<CompetitionSmallTeamLoLInfoDTO> res = new ArrayList<>();
        CompetitionSmallTeamLoLInfoDTO team1 = new CompetitionSmallTeamLoLInfoDTO();
        team1.setTeamName(vo.getTeamOneName());
        CompetitionSmallTeamLoLInfoDTO team2 = new CompetitionSmallTeamLoLInfoDTO();
        team2.setTeamName(vo.getTeamTowName());
        res.add(team1);
        res.add(team2);
        return res;
    }
}
